<?php
/**
 * @file starslistcontroller.php $
 * @author LinLin (linlin@haibao.com) $
 * @date 2015-11-30 上午11:59:07 $
 * @brief  $
 */
namespace haibao\cms\business\control\pc\channel\stars;

use haibao\cms\data\control\stars\StarsController as StarsControllerData;
use haibao\cms\model\filter\Celebrity as CelebrityFilter;
use haibao\cms\model\filter\CelebrityRiseCount as CelebrityRiseCountFilter;
use haibao\cms\model\filter\CelebrityImage as CelebrityImageFilter;
use haibao\cms\business\control\ArticleController;
use haibao\cms\model\data\Celebrity as CelebrityModel;
use haibao\cms\crontab\celebrityRiseCount;
use haibao\frame\data\query\Condition;
use haibao\frame\data\query\Pager;

class StarsController extends \haibao\cms\business\control\pc\channel\ChannelController{
	
	const STAR_QUESS_QUESS_QUESS = '8335';
	const FIRST_CELEBRITY_PIC = '0';
	const PAGE_SIZE = 30;
	const STAR_A_Z_OTHER_LETTER = 'OTHER';
	const STAR_SREACH_KEY= 'STAR';
	private static $_instance;
	public $starsData;
	public function __construct($updateTime='',$controlId='',$isPublish=''){
		if(!$this->starsData instanceof StarsController){
			$this->starsData = new StarsControllerData;
		}
		parent::__construct($updateTime,$controlId,$isPublish);
	}
	
	public static function getStarsInstance(){
		if(!(self::$_instance instanceof self)){
			self::$_instance = new self;
		}
		return self::$_instance;
	}
	
	/**
	 * 获取今日推荐明星数据
	 */

	public function getRecommendStars($nowDate,$celebrityIdArr = array(),$limit){
		$celebrityList = $this->starsData->getTodayRecommendStars($nowDate,$celebrityIdArr,$limit);
		//$celebrityList = $this->starsData->getTodayRecommendStars($nowDate);
		$date = new \DateTime();
		$nowDate = $date->format('Y-m-d');
 		$nowMonth = date('m', strtotime($nowDate));
 		$nowDay = date('d', strtotime($nowDate));
 		$XingZuo = \haibao\cms\business\Celebrity::getStarXingZuo($nowMonth,$nowDay);
		if($celebrityList){
			$starTagIdArr = array_keys($celebrityList);
			$articleArr = $imageCollArr = $imageArticleIdArr = array();
			$infoStarArr = $this->starsData->getInfoStars($starTagIdArr);
			$starUrlInfo = $this->starsData->getStarDetailByTagId($starTagIdArr);
			foreach($celebrityList as $starTagId=>$star){
				if(isset($starUrlInfo[$starTagId])){
					$celebrityList[$starTagId]['StarUrl'] = \haibao\classlibrary\Config::getConfig(\haibao\classlibrary\Config::CLASSLIBRARY_CONFIG_HOST_STARS).$starUrlInfo[$starTagId]['Url'];
				}
				if(isset($infoStarArr[$starTagId])){
					$celebrityList[$starTagId]['InfoList'] = $infoStarArr[$starTagId];
					foreach($infoStarArr[$starTagId] as $k=>$info){
						if($info == 1){
							array_push($articleArr,$k);
						}else{
							array_push($imageCollArr,$k);
						}
					}
				}else{
					$celebrityList[$starTagId]['InfoList'] = array();
				}
			}
			list($articleList,$imageCollList) = $this->starsData->getRecommendStarsArticle($articleArr,$imageCollArr);
			$starAvatorList = $this->starsData->getStarInfoImg($starTagIdArr);
			foreach($celebrityList as $starTagId=>$star){
				if(isset($star['IsBirthday']) && $star['IsBirthday'] == 1){
					$celebrityList[$starTagId]['XingZuo'] = $XingZuo;
				}else if(isset($star['Birthday'])){
					$birthMonth = date('m', strtotime($star['Birthday']));
					$birthDay = date('d', strtotime($star['Birthday']));
					$celebrityList[$starTagId]['XingZuo'] = \haibao\cms\business\Celebrity::getStarXingZuo($birthMonth,$birthDay);
				}
				foreach($star['InfoList'] as $k=>$info){
					if($info == 1){
						if(isset($articleList[$k])){
							$articleInfo = $articleList[$k];
							//$articleInfo['Url'] = $this->getTagChannel($articleInfo['ParentId']).'/article/'.$articleInfo['PublishId'].'.htm';
							$articleInfo['Url'] = $this->getArticleUrl($articleInfo['PublishId'],$articleInfo['ParentId']);
							$celebrityList[$starTagId]['InfoList'][$k] = $articleInfo;
						}else {
							unset($celebrityList[$starTagId]['InfoList'][$k]);
						}
					}else{
						if(isset($imageCollList[$k])){
							$imageCollInfo = $imageCollList[$k];
							//$imageCollInfo['Url'] = $this->getTagChannel($imageCollInfo['ParentId']).'/article/'.$imageCollInfo['Id'].'.htm';
							$imageCollInfo['Url'] = $this->getArticleUrl($imageCollInfo['Id'],$imageCollInfo['ParentId'],ArticleController::INFO_TYPE_IMAGE);
							$celebrityList[$starTagId]['InfoList'][$k] = $imageCollInfo;
						}else {
							unset($celebrityList[$starTagId]['InfoList'][$k]);
						}
					}
				}
				
				if(isset($starAvatorList[$starTagId])){
					$celebrityList[$starTagId]['ImageUrl'] =self::getImageLink($starAvatorList[$starTagId]['ImageUrl'],160,160);
					$celebrityList[$starTagId]['ImageUrlBig'] = self::getImageLink($starAvatorList[$starTagId]['ImageUrl'],260,260);
				}
			}
			return $celebrityList;
		}
	}
	
	/**
	 * 获取明星信息
	 * @param string $starType 明星类别
	 * @param string $gender 明星性别
	 * @return Array 明星地址+头像地址
	 */
	
	public function getPopularityStar($starType= null,$gender=null){
		$popularityStarResult = $this->starsData->getPopularityStar($starType,$gender);
		foreach($popularityStarResult as $key=>$val){
			if($key == self::FIRST_CELEBRITY_PIC){
				$popularityStarResult[$key]['ImageUrl'] = self::getImageLink($val['ImageUrl'],220,293);
			}else{
				$popularityStarResult[$key]['ImageUrl'] = self::getImageLink($val['ImageUrl'],160,160);
			}
		}
		return $popularityStarResult;
	}

	public function getStarChangeList($change,$userId){
		$loveStar = $this->starsData->getStarMached($change,$userId);
		if(!empty($loveStar)){
			$num = $loveStar['Match']+$loveStar['NoMatch'];
			if($num != 0){
				$matPer = round(($loveStar['Match']/$num)*100).'%';
				$noMatPer = round(($loveStar['NoMatch']/$num)*100).'%';
			}else{
				$matPer = $noMatPer = 0;
			}
			$loveStar['MatchPer'] = $matPer;
			$loveStar['NoMatchPer'] = $noMatPer;
			if($loveStar){
				$loveStarInfo = $this->starsData->getStarLoveInfo($loveStar);
				$n = 1;
				foreach($loveStarInfo as $key=>$val){
					if(isset($val['ImageUrl'])){
						$loveStarInfo[$key]['ImageUrl'] = self::getImageLink($val['ImageUrl'],160,160);//明星信息
					}else{
						$loveStarInfo[$key]['ImageUrl'] = '';
					}
					$loveStarInfo[$key]['Sort'] = $n++;
				}
			}
			return array_merge($loveStar,$loveStarInfo);
		}
		return array();
	}
	
	/**
	 * @param Array $condition 查询条件
	 * @return Object $starList 明星列表
	 */
	public function getStarList($condition,$isGetAll = true){
		// 根据$confition 封装query filter
		if(!$isGetAll){
			$dataList = $this->getStarListByCondition($condition);
		}else{
			$dataList = \haibao\www\cache\Library::getSearchCache(self::STAR_SREACH_KEY);
			if(isset($dataList)){	
				$dataList = json_decode($dataList,true);
			}else{
				$dataList = $this->getStarListAZ();
				\haibao\www\cache\Library::setSearchCache(self::STAR_SREACH_KEY,json_encode($dataList));
			}
		}
		return $dataList;
	}
	
	public function getStarListAZ(){
		$starList = $pager = $starFans = array();
		$preFiveStarIdArr = $allStarTagIdArr = array();
		// 获取明星A_Z数据
		$count = 0;
		for($start=ord("A");$start<=ord("Z");$start++){
			$letterStarList = $this->starsData->getStarListByLetter(chr($start));
			if($letterStarList){
				$preFiveStar = array_slice($letterStarList, 0,5);
				$nextNineStar = array_slice($letterStarList, 5,9);
				/* if (count($letterStarList) < 14){
				 echo count($letterStarList);
				echo chr($start);
				print_r($preFiveStar);
				print_r($nextNineStar);exit;
				} */
				$starList[chr($start)]['five'] = $preFiveStar;
				$starList[chr($start)]['nine'] = $nextNineStar;
				$count += count($letterStarList);
				$preFiveStarIdArr = array_merge($preFiveStarIdArr,array_map(function($element){return $element['TagId'];}, $preFiveStar));
				$allStarTagIdArr =  array_merge($allStarTagIdArr,array_map(function($element){return $element['TagId'];}, $letterStarList));
			}
		}
		
		$letterStarList = $this->starsData->getStarListByLetter(self::STAR_A_Z_OTHER_LETTER);
		if($letterStarList){
			$preFiveStar = array_slice($letterStarList, 0,5);
			$nextNineStar = array_slice($letterStarList, 5,9);
			$starList[self::STAR_A_Z_OTHER_LETTER]['five'] = $preFiveStar;
			$starList[self::STAR_A_Z_OTHER_LETTER]['nine'] = $nextNineStar;
			$count += count($letterStarList);
			$preFiveStarIdArr = array_merge($preFiveStarIdArr,array_map(function($element){return $element['TagId'];}, $preFiveStar));
			$allStarTagIdArr =  array_merge($allStarTagIdArr,array_map(function($element){return $element['TagId'];}, $letterStarList));
		}
	
		$starAvatorList = $this->starsData->getStarInfoImg($preFiveStarIdArr);
		$starUrlList = $this->starsData->getStarDetailByTagId($allStarTagIdArr);
		//$starFansCount = $this->starsData->getStarFansByTagIdArr($preFiveStarIdArr);
		foreach($starList as $letter=>$star){
			$preFiveStarList = $starList[$letter]['five'];
			$nextNineStarList = $starList[$letter]['nine'];
			foreach($preFiveStarList as $key=>$preFiveStar){
				$preFiveStarList[$key]['ImageUrl'] = $preFiveStarList[$key]['StarUrl'] = '';
				if(isset($starAvatorList[$preFiveStar['TagId']])){
					$preFiveStarList[$key]['ImageUrl'] = self::getImageLink($starAvatorList[$preFiveStar['TagId']]['ImageUrl'],160,160);
				}
				if(isset($starUrlList[$preFiveStar['TagId']])){
					$preFiveStarList[$key]['StarUrl'] = \haibao\classlibrary\Config::getConfig(\haibao\classlibrary\Config::CLASSLIBRARY_CONFIG_HOST_STARS).$starUrlList[$preFiveStar['TagId']]['Url'];
				}
				if(!isset($preFiveStarList[$key]['FansCount'])){
					$preFiveStarList[$key]['FansCount'] = 0;
				}
			}
			foreach($nextNineStarList as $key=>$nextNineStar){
				$nextNineStarList[$key]['StarUrl'] = '';
				if(isset($starUrlList[$nextNineStar['TagId']])){
					$nextNineStarList[$key]['StarUrl'] = \haibao\classlibrary\Config::getConfig(\haibao\classlibrary\Config::CLASSLIBRARY_CONFIG_HOST_STARS).$starUrlList[$nextNineStar['TagId']]['Url'];
				}
			}
			$starList[$letter]['five'] = $preFiveStarList;
			$starList[$letter]['nine'] = $nextNineStarList;
		}
		return array($starList,$pager);
	}
	
	
	public function getStarListByCondition($condition){
		$celebrityFilter = new CelebrityFilter();
		$celebrityRiseCountFilter = new CelebrityRiseCountFilter();
		$celebrityFilter->select(array(
				CelebrityFilter::CONDITION_FIELD_NAME_ID,
				CelebrityFilter::CONDITION_FIELD_NAME_TAG_ID,
				CelebrityFilter::CONDITION_FIELD_NAME_NAME_CN,
				CelebrityFilter::CONDITION_FIELD_NAME_NAME_EN,
				CelebrityFilter::CONDITION_FIELD_NAME_GENDER,
				CelebrityFilter::CONDITION_FIELD_NAME_PLACE,
				CelebrityFilter::CONDITION_FIELD_NAME_STAR_TYPES,
				CelebrityFilter::CONDITION_FIELD_NAME_PINYIN_EN,
				CelebrityFilter::CONDITION_FIELD_NAME_ADD_TIME,
		));
		$celebrityRiseCountFilter->select(array(
				CelebrityRiseCountFilter::CONDITION_FIELD_NAME_FANS_COUNT
		));
		//$celebrityFilter->limit(0,30);
		if($condition['state']){
			if($condition['state'] == CelebrityModel::SITE_STATE_FANS_MORE){
				$celebrityFilter->order(CelebrityRiseCountFilter::CONDITION_FIELD_NAME_FANS_COUNT,false,$celebrityRiseCountFilter);
			}else{
				$celebrityFilter->order(CelebrityFilter::CONDITION_FIELD_NAME_ADD_TIME,false);
			}
		}
		$celebrityFilter->innerJoin($celebrityRiseCountFilter,null,CelebrityFilter::CONDITION_FIELD_NAME_ID,CelebrityRiseCountFilter::CONDITION_FIELD_NAME_CELEBRITY_ID,CelebrityFilter::CONDITION_FIELD_NAME_CELEBRITY_RISECOUNT,false);
		if($condition['gender']){
			$celebrityFilter->where(CelebrityFilter::CONDITION_FIELD_NAME_GENDER,Condition::CONDITION_EQUAL,(int)$condition['gender']);
		}
		
		if($condition['area']){
			$celebrityFilter->where(CelebrityFilter::CONDITION_FIELD_NAME_PLACE,Condition::CONDITION_EQUAL,(int)$condition['area']);
		}
			
		if($condition['category']){
			$celebrityFilter->where(CelebrityFilter::CONDITION_FIELD_NAME_STAR_TYPES,Condition::CONDITION_LIKE,'%['.$condition['category'].']%');
		}
			
		if($condition['letter']){
			if($condition['letter'] === self::STAR_A_Z_OTHER_LETTER){
				$enRange = array();
				for($i=ord('A');$i<=ord('Z');$i++){
					array_push($enRange,chr($i));
				}
				$celebrityFilter->where(CelebrityFilter::CONDITION_FIELD_NAME_PINYIN_EN,Condition::CONDITION_NOTIN,$enRange);
			}else{
				$celebrityFilter->where(CelebrityFilter::CONDITION_FIELD_NAME_PINYIN_EN,Condition::CONDITION_EQUAL,$condition['letter']);
			}
		
		}
		$celebrityFilter->where(CelebrityFilter::CONDITION_FIELD_NAME_STATUS,Condition::CONDITION_EQUAL,\haibao\cms\model\data\Celebrity::STATUS_ENABLE);
		$celebrityFilter->setPager(new Pager(self::PAGE_SIZE,$condition['page']));
		$pager = $celebrityFilter->getPager();

		$starList = \haibao\cms\business\Celebrity::getAll($celebrityFilter);
		$starAvatorList = $starTagInfoList = $starTagIdArr = array();
		if($starList){
			foreach($starList as $star){
				array_push($starTagIdArr,$star->TagId);
			}
			$starAvatorList = $this->starsData->getStarInfoImg($starTagIdArr);
			$starTagInfoList = $this->starsData->getStarDetailByTagId($starTagIdArr);
			foreach($starList as $star){
				if(isset($starTagInfoList[$star->TagId])){
					$star->TopicId = \haibao\classlibrary\Config::getConfig(\haibao\classlibrary\Config::CLASSLIBRARY_CONFIG_HOST_STARS).'/star/'.$starTagInfoList[$star->TagId]['TopicId'].'/';
					if(isset($starAvatorList[$star->TagId])){
						$star->ImageSrc = self::getImageLink($starAvatorList[$star->TagId]['ImageUrl'],160,160);
					}
				}
			}
		}
		return array($starList,$pager);
	}
}